package com.keshe.member.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keshe.common.utils.PageUtils;
import com.keshe.common.utils.Query;

import com.keshe.member.dao.UserDao;
import com.keshe.member.entity.UserEntity;
import com.keshe.member.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        //    获取key
        String key = (String) params.get("key");

        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        //    key值不为空，判断account或phone是否相同
        if (!StringUtils.isEmpty((key))) {
            queryWrapper.eq("account", key).or().eq("phone", key);
        }


        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    // 通过用户名查询用户
    @Override
    public UserEntity queryByAccount(String account) {
        return baseMapper.queryByAccount(account);
    }

    // 通过手机号查询用户
    @Override
    public UserEntity queryByPhone(String phone) {
        return baseMapper.queryByPhone(phone);
    }

    // 通过邮箱查询用户
    @Override
    public UserEntity queryByEmail(String email) {
        return baseMapper.queryByEmail(email);
    }
}

